package applogic;

import java.util.StringTokenizer;

public class Appointment
{
	protected int day;
	protected int month;
	protected int start;
	protected int end;
	protected int ID;
	protected String description;
	protected String place;
	protected User owner;
	public Appointment()
	{
		day = 0;
		month = 0;
		start = 0;
		end = 0;
		ID = 0;
		description = null;
		place = null;

		/* todo: */
		//owner = CalendarClient.client.user;
	}
	
	public Appointment(String dbTuple)
	{
		/* Constructs appointment object from a db tuple
		 * TODO: actually implement, when i know exactly how it'll look.
		 */
	}
	
	public Appointment(int ID, int day, int month, int start, int end, String description, String place, User holds)
	{
		this.day = day;
		this.month = month;
		this.start = start;
		this.end = end;
		this.ID = ID;
		this.description = description;
		this.place = place;
		this.owner = holds;
	}
	
	public void buildFromNetMsg(String content)
	{
		StringTokenizer st = new StringTokenizer(content,",");
		
		//Construct meeting from message.
		//"appointment_storage_request,#day,#month,#start,#end,#ID,description,place,owner";
		day = Integer.parseInt(st.nextToken());
		month = Integer.parseInt(st.nextToken());
		start = Integer.parseInt(st.nextToken());
		end = Integer.parseInt(st.nextToken());
		ID = Integer.parseInt(st.nextToken());
		String tmp = st.nextToken("").substring(1);
		description = tmp.substring(0,64);
		description = description.substring(0,description.lastIndexOf(" "));
		place = tmp.substring(65,129);
		place = place.substring(0,description.lastIndexOf(" "));
		owner = new User(tmp.substring(130),"");
	}
	
	public void setID(int ID)
	{
		this.ID = ID;
	}
	
	public int getStart()
	{
		return start;
	}
	
	public int getDay()
	{
		return day;
	}
	
	public int getEnd()
	{
		return end;
	}
	
	public int getMonth()
	{
		return month;
	}

	public void setStart(int time)
	{
		start = time;
	}

	public void setEnd(int time)
	{
		end = time;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public void setPlace(String place)
	{
		this.place = place;
	}

	public void deleteAppointment()
	{
		/*todo: send message to server that appointment ID
				is to be remove from database.*/
	}
	
	public void setDate(int day, int month)
	{
		/* presumes correct input */
		this.day = day;
		this.month = month;
	}
	
	public void setOwner(User user)
	{
		owner=user;
	}
	
	public int getID()
	{
		return ID;
	}
	
	public String getDescription(){
		return description;
		
	}
	
	public String toString()
	{
		return  "ID: "+ID+"\n"
				+"Date: "+day+" of "+month+"\n"
				+"From "+start+" to "+end+"\n"
				+"Description: "+description+"\n"
				+"At: "+place+"\n"
				+"Owner: "+owner;
	}

	public String toMessage() {
		char[] descrpt = new char[64];
		int a=0;
		for(;a<description.length()&&a<64;a++)descrpt[a]=description.charAt(a);
		for(;a<64;a++)descrpt[a]=32;
		
		char[] plc = new char[64];
		a=0;
		for(;a<place.length()&&a<64;a++)plc[a]=place.charAt(a);
		for(;a<64;a++)plc[a]=32;
		return day+","+month+","+start+","+end+","+ID+","+new String(descrpt)+","+new String(plc)+","+owner;
	}
	
	public String toSQL(){
		return "INSERT INTO Appointment(AID, Day, Month, Start, Finish, Description, placeDescription, holds)" +
				" VALUES("+ID+","+day+","+month+","+start+","+end+",'"+description+"', '"+place+"','"+owner+"')";
	}
	public User getOwner()
	{
		return owner;
	}

	public String getPlace() {
		return place;
	}
}